<?php
class ModelCatalogMenu extends Model {
	public function addMenu($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "menu SET parent_id = '" . (int)$data['parent_id'] . "', url='".$this->db->escape($data['url'])."', new='".(int)$data['display']."', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "', date_modified = NOW(), date_added = NOW()");
	
		$menu_id = $this->db->getLastId();
		
		
		foreach ($data['menu_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "menu_description SET menu_id = '" . (int)$menu_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
	
						
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'menu_id=" . (int)$menu_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		
	}
	
	public function editMenu($menu_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "menu SET parent_id = '" . (int)$data['parent_id'] . "', url='".$this->db->escape($data['url'])."', new='".(int)$data['display']."', sort_order = '" . (int)$data['sort_order'] . "', status = '" . (int)$data['status'] . "', date_modified = NOW() WHERE menu_id = '" . (int)$menu_id . "'");
		

		$this->db->query("DELETE FROM " . DB_PREFIX . "menu_description WHERE menu_id = '" . (int)$menu_id . "'");

		foreach ($data['menu_description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "menu_description SET menu_id = '" . (int)$menu_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'menu_id=" . (int)$menu_id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'menu_id=" . (int)$menu_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		
		
	}
	
	public function deleteMenu($menu_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "menu WHERE menu_id = '" . (int)$menu_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "menu_description WHERE menu_id = '" . (int)$menu_id . "'");		
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'menu_id=" . (int)$menu_id . "'");
		
		$query = $this->db->query("SELECT menu_id FROM " . DB_PREFIX . "menu WHERE parent_id = '" . (int)$menu_id . "'");

		foreach ($query->rows as $result) {
			$this->deleteMenu($result['menu_id']);
		}
		

	} 

	public function getMenu($menu_id) {
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'menu_id=" . (int)$menu_id . "') AS keyword FROM " . DB_PREFIX . "menu WHERE menu_id = '" . (int)$menu_id . "'");
		
		return $query->row;
	} 
	
	public function getCategories($parent_id = 0) {
		
	
		
			$menu_data = array();
		
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "menu c LEFT JOIN " . DB_PREFIX . "menu_description cd ON (c.menu_id = cd.menu_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
		
			foreach ($query->rows as $result) {
				$menu_data[] = array(
					'menu_id' => $result['menu_id'],
					'name'        => $this->getPath($result['menu_id'], $this->config->get('config_language_id')),
					'status'  	  => $result['status'],
					'sort_order'  => $result['sort_order']
				);
			
				$menu_data = array_merge($menu_data, $this->getCategories($result['menu_id']));
			}	
	
			
		
		
		return $menu_data;
	}
	
	public function getPath($menu_id) {
		$query = $this->db->query("SELECT name, parent_id FROM " . DB_PREFIX . "menu c LEFT JOIN " . DB_PREFIX . "menu_description cd ON (c.menu_id = cd.menu_id) WHERE c.menu_id = '" . (int)$menu_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
		
		if ($query->row['parent_id']) {
			return $this->getPath($query->row['parent_id'], $this->config->get('config_language_id')) . $this->language->get('text_separator') . $query->row['name'];
		} else {
			return $query->row['name'];
		}
	}
	
	public function getMenuDescriptions($menu_id) {
		$menu_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "menu_description WHERE menu_id = '" . (int)$menu_id . "'");
		
		foreach ($query->rows as $result) {
			$menu_description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description'],
				'description'      => $result['description']
			);
		}
		
		return $menu_description_data;
	}	
	
	
		
	public function getTotalCategories() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "menu");
		
		return $query->row['total'];
	}	
		
	public function getTotalCategoriesByImageId($image_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "menu WHERE image_id = '" . (int)$image_id . "'");
		
		return $query->row['total'];
	}

		
}
?>